package com.donglan.pethome.product.service.impl;

import com.donglan.pethome.basic.service.impl.BaseServiceImpl;
import com.donglan.pethome.product.domain.Product;
import com.donglan.pethome.product.domain.ProductDetail;
import com.donglan.pethome.product.mapper.ProductDetailMapper;
import com.donglan.pethome.product.mapper.ProductMapper;
import com.donglan.pethome.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: zhangdonghui
 * @Date: 2022/2/8 11:09
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;


    /*
     * 功能描述 添加或者修改
     * @author zhangdonghui
     * @date 2022/2/8 11:28
     * @param product
     * @return void
     */
    @Override
    @Transactional
    public void addorUpdate(Product product) {
        if(product.getId() == null){
            super.add(product);
            //获取服务明细
            ProductDetail productDetail = product.getProductDetail();
            //设置服务明细
            productDetail.setProduct(product);
            //保存服务明细
            productDetailMapper.add(productDetail);

        } else {
            super.update(product);
            //获取服务明细
            ProductDetail productDetail = product.getProductDetail();
            //设置服务明细
            productDetail.setProduct(product);
            //保存服务明细
            productDetailMapper.update(productDetail);

        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 根据服务id删除服务对象
        super.delete(id);
        // 根据服务id删除服务明细对象
        productDetailMapper.deleteByProductId(id);
    }

    @Override
    @Transactional
    public void batchRemove(List<Product> products) {
        //批量删除服务产品
        super.batchRemove(products);
        //批量删除服务明细
        productDetailMapper.batchRemoveByProducts(products);
    }

    @Override
    @Transactional
    public void batchOnSale(List<Product> products) {
        productMapper.batchOnSale(products);
    }

}
